package com.nuc.seckill.rocketmq.util;

import com.alibaba.fastjson.JSON;
import com.nuc.seckill.base.SendMsgReq;
import com.nuc.seckill.enums.MqNodesEnum;
import com.nuc.seckill.rocketmq.config.MqConfig;
import com.nuc.seckill.rocketmq.config.MqNode;
import com.nuc.seckill.rocketmq.producer.RocketProducer;
import com.nuc.seckill.rocketmq.producer.SpringProducer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 消息发送者
 *
 * @author 石一歌
 * @date 2022-08-23 23:24
 */
@Component
public class MqSender {

    @Resource
    RocketProducer rocketProducer;

    @Resource
    MqConfig mqConfig;

    @Resource
    SpringProducer springProducer;

    public void sendDelCacheMsg(SendMsgReq.DelCacheReq req) {
        MqNode mqNode = mqConfig.getMqNode(MqNodesEnum.delCache);
        rocketProducer.sendMsg(mqNode, "productId".concat(req.getProductId().toString()), JSON.toJSONString(req));
    }

    public void sendOrderMsg(SendMsgReq.OrderReq req) {
        MqNode mqNode = mqConfig.getMqNode(MqNodesEnum.order);
        rocketProducer.sendMsg(mqNode, "productId".concat(req.getProductId().toString()).concat("userId").concat(req
                .getUserId().toString()), JSON.toJSONString(req));
    }

    public void sendDelCacheMsgByAsync(SendMsgReq.DelCacheReq req) {
        MqNode mqNode = mqConfig.getMqNode(MqNodesEnum.delCache);
        springProducer.sendMessage(mqNode.getTopic(), mqNode.getTag(), JSON.toJSONString(req));
    }

    public void sendOrderMsgByAsync(SendMsgReq.OrderReq req) {
        MqNode mqNode = mqConfig.getMqNode(MqNodesEnum.order);
        springProducer.sendMessage(mqNode.getTopic(), mqNode.getTag(), JSON.toJSONString(req));
    }
}

